<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

use app\common\logic\Checkin as CommonCheckin;

/**
 * 签到管理逻辑
 */
class Checkin extends AdminBase
{
    
    public static $commonCheckinLogic = null;
    
    /**
     * 基类初始化
     */
    public function __construct()
    {
        // 执行父类构造方法
        parent::__construct();
        
        empty(static::$commonCheckinLogic) && static::$commonCheckinLogic = new CommonCheckin();
    }
    
    /**
     * 获取签到列表搜索条件
     * @param array $data 搜索参数
     * @return array
     */
    public function getWhere($data = [])
    {
        return static::$commonCheckinLogic->getWhere($data);
    }
    
    /**
     * 获取签到列表
     */
    public function getCheckinList($where = [], $field = true, $order = 'checkin_time desc', $paginate = DB_LIST_ROWS)
    {
        return static::$commonCheckinLogic->getCheckinList($where, $field, $order, $paginate);
    }
    
    /**
     * 获取签到信息
     */
    public function getCheckinInfo($where = [], $field = true)
    {
        return static::$commonCheckinLogic->getCheckinInfo($where, $field);
    }
    
    /**
     * 添加签到记录
     */
    public function checkinAdd($data = [])
    {
        $url = url('index');
        
        $validate_result = $this->validateCheckin->scene('add')->check($data);
        
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateCheckin->getError()];
        }
        
        $result = static::$commonCheckinLogic->checkinAdd($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('新增', '新增签到记录，用户ID：' . $data['user_id']);
        
        return [RESULT_SUCCESS, '添加成功', $url];
    }
    
    /**
     * 编辑签到记录
     */
    public function checkinEdit($data = [])
    {
        $url = url('index');
        
        $validate_result = $this->validateCheckin->scene('edit')->check($data);
        
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateCheckin->getError()];
        }
        
        $result = static::$commonCheckinLogic->checkinEdit($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('编辑', '编辑签到记录，id：' . $data['id']);
        
        return [RESULT_SUCCESS, '编辑成功', $url];
    }
    
    /**
     * 删除签到记录
     */
    public function checkinDel($data = [])
    {
        $url = url('index');
        
        if (empty($data['id'])) {
            return [RESULT_ERROR, '参数错误'];
        }
        
        $result = static::$commonCheckinLogic->checkinDel($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('删除', '删除签到记录，id：' . $data['id']);
        
        return [RESULT_SUCCESS, '删除成功', $url];
    }
    
    /**
     * 获取签到统计
     */
    public function getCheckinStats($data = [])
    {
        return static::$commonCheckinLogic->getCheckinStats($data);
    }
    
    /**
     * 导出签到列表
     */
    public function exportCheckinList($where = [], $field = true, $order = 'checkin_time desc')
    {
        $list = static::$commonCheckinLogic->getCheckinList($where, $field, $order, false);
        
        $titles = "用户ID,签到时间,获得积分,连续签到天数,签到IP";
        $keys   = "user_id,checkin_time,points,continuous_days,checkin_ip";
        
        action_log('导出', '导出签到列表');
        
        export_excel($titles, $keys, $list, '签到列表');
    }
    
    /**
     * 签到设置
     */
    public function checkinSettings($data = [])
    {
        $url = url('settings');
        
        $result = static::$commonCheckinLogic->checkinSettings($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('设置', '签到设置更新');
        
        return [RESULT_SUCCESS, '设置保存成功', $url];
    }
    
    /**
     * 获取签到设置
     */
    public function getCheckinSettings()
    {
        return static::$commonCheckinLogic->getCheckinSettings();
    }
    
    /**
     * 重置用户签到
     */
    public function resetUserCheckin($data = [])
    {
        $url = url('index');
        
        if (empty($data['user_id'])) {
            return [RESULT_ERROR, '用户ID不能为空'];
        }
        
        $result = static::$commonCheckinLogic->resetUserCheckin($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('重置', '重置用户签到，用户ID：' . $data['user_id']);
        
        return [RESULT_SUCCESS, '重置成功', $url];
    }
}